<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 16:58
 * 采购管理
 */
class procurement extends MY_Controller
{

    public function __construct()
    {

        parent::__construct();
        $this->load->model('procurement_model');
        $this->load->model('purchaseOrder_model');

    }

    /**
     *
    点击新增需求单，进入新增需求单页面   已完成
    点击需求单编号，进入需求单详情页     已完成
    所以的需求单在未进行进货前都可以作废，点击作废，状态变为已作废   下午增加是否开始进货了
    审核通过的采购需求单，点击去进货，进入采购进货单页面
    未提交的需求单可以进行再次编辑，点击编辑，进入采购需求单编辑页面   判断是否提交
    采购需求单未提交前，可以删除，提交后只能进行作废            判断是否提交
    当前页面数据超过20条时，翻页显示     已完成
     */

    //采购管理
    public function index()
    {
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->procurement_model->search('*',0,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('admin/procurement/list',$viewData);
        } else {
            $data = $this->procurement_model->page('procurement',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('admin/procurement/list',$viewData);
        }
    }

    //新增采购需求单功能
    public function add(){

        $data = [
            'code' => 'xqd'.date('ymd').rand(100,999),
            'source' => trim($this->input->post('source')),
            'content' => trim($this->input->post('content')),
            'name' => trim($this->input->post('name')),
            'contactMan' => trim($this->input->post('contactMan')),
            'director' => trim($this->input->post('director')),
            'president' => trim($this->input->post('president')),
            'chairman' => trim($this->input->post('chairman')),
            'financial' => trim($this->input->post('financial')),
            'endTime' => trim(strtotime($this->input->post('endTime'))),
            'createTime'=> trim(strtotime($this->input->post('createTime'))),
            'type' => '采购需求单'
        ];

        if($this->input->post('action') == '暂存'){
            //状态为0 待提交
            $data['status'] = 0;
        }else{
            //状态为1 审核中
            $data['status'] = 1;
        }

        $resultData =  $this->procurement_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/procurement/index');
        }
    }

    //新增页面
    public function addView(){

        $this->load->view('/admin/procurement/add');
    }

    //编辑功能
    public function edit(){
        //用户id
//        print_r($_POST);
//        die();

        //todo 更多字段
        $data = [
            'source' => trim($this->input->post('source')),
            'content' => trim($this->input->post('content')),
            'name' => trim($this->input->post('name')),
            'contactMan' => trim($this->input->post('contactMan')),
            'director' => trim($this->input->post('director')),
            'president' => trim($this->input->post('president')),
            'chairman' => trim($this->input->post('chairman')),
            'financial' => trim($this->input->post('financial')),
            'endTime' => trim(strtotime($this->input->post('endTime'))),
            'createTime'=> trim(strtotime($this->input->post('createTime'))),
            'type' => '采购需求单'
        ];

        $where = [
            'id' => $this->input->post('id')
        ];

//        print_r($data);
//        die();

        //是否会有添加失败的情况?
        $resultData =  $this->procurement_model->edit($data,$where);

//        print_r($resultData);
//        die();

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/procurement/index');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->procurement_model->getByFieldQueryData('*',$arg,0);

//        print_r($data);
//        die();

        $this->load->view('/admin/procurement/edit',$data);
    }

    //删除功能
    public function remove(){
        $id = $this->input->get('id');

        $where = [
            'id' => $id
        ];

        $response = $this->procurement_model->remove($where);

        if($response){
            redirect('/admin/procurement/index');

        }
    }

    //导出功能
    public function daochu(){

        $this->load->library('Excels');

        //表格标题
        $title = [
            0 => [
                0 => '序号',
                1 => '采购编码',
                2 => '采购名字',
                3 => '采购简称',
                4 => '采购联络人',
                5 => '采购联系人',
                6 => '联系号码',
                7 => '地址',
                8 => '创建时间',
                9 => 'size',
                10 => 'nature',
                11 => 'isProducers',
                12 => 'vender',
                13 => 'mail',
                14 => 'note',
            ],
        ];

        $data = $this->procurement_model->getData('*',1);

        //表格内容
//        $content =  [
//            0 => [
//                0 => '序号',
//                1 => '采购编码',
//                2 => '采购名字',
//                3 => '采购简称',
//                4 => '采购联络人',
//                5 => '采购联系人',
//                6 => '联系号码',
//                7 => '地址',
//                8 => '创建时间',
//                9 => 'size',
//                10 => 'nature',
//                11 => 'isProducers',
//                12 => 'vender',
//                13 => 'mail',
//                14 => 'note',
//        ],
//            1 => [
//                0 => '12312人',
//                1 => '所发生的丰富的',
//                2 => '积分高级人机'
//        ]];

        //二维数据
        $this->excels->exports($data,$title);

    }

    //查看详情
    public function detail(){
        $this->load->view('/admin/procurement/detail');
    }

    //作废功能
    public function invalid(){

        $data = [
            'status' => 3 //作废功能
        ];

        $where = [
            'id' => $this->input->get('id'),
        ];


        $data = $this->procurement_model->edit($data,$where);

        if($data){
            redirect('/admin/procurement/index');

        }
    }


    //进货单列表页面
    public function purchaseOrder()
    {
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }


        $status = (isset($_GET['status'])) ? trim($_GET['status']) : 123;

//        print_r($status);
//        die();
//

        //判断搜索内容是否存在存在就继续
        if ($this->input->get('search')) {
            $searchArg = [
                'field' => 'name',
                'arg' => $this->input->get('search'),
                'side' => 'both'
            ];

            if ($status == '123') {
                $where = 0;
            } elseif ($status == 'w') {
                $where = [
                    'status =' => 0
                ];
            } else {
                $where = [
                    'status !=' => 0
                ];
            }

//            print_r($where);
//            die();

            $data = $this->purchaseOrder_model->search('*', $where, $searchArg, $page_num, $start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条
            ];
            $this->load->view('admin/procurement/purchaseOrder/list', $viewData);
        } else {

            if ($status == '123') {
                $where = 0;
            } elseif ($status == 'w') {
                $where = [
                    'status =' => 0
                ];
            } else {
                $where = [
                    'status !=' => 0
                ];
            }

//            print_r($where);
//            die();


            $data = $this->purchaseOrder_model->page('purchaseOrder', $where, $page_num, $start_position); //limit查询的数据

//            print_r($data);
//            die();

            if ($where) {
                $viewData = [
                    'list' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条

                ];
            } else {
                $viewData = [
                    'list' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条

                ];
            }

            //  调试所有sql数据
            //  $this->output->enable_profiler(TRUE);


            $this->load->view('admin/procurement/purchaseOrder/list', $viewData);
        }
    }

    //进货单新增页面
    public function purchaseOrderAddView(){
        $this->load->view('/admin/procurement/purchaseOrder/add');

    }

    //进货单新增功能
    public function purchaseOrderAdd(){
        //合同信息
        $contracInfo = [
            'amountSmall' => trim($this->input->post('amountSmall')),
            'deliveryType' => trim($this->input->post('deliveryType')),
            'freight' => trim($this->input->post('freight')),
            'jiaohuoTime' => trim($this->input->post('jiaohuoTime')),
            'payWay' => trim($this->input->post('payWay')),
            'address' => trim($this->input->post('address')),
            'amountBig' => trim($this->input->post('amountBig')),
            'deliveryAddress' => trim($this->input->post('deliveryAddress')),
            'logistics' => trim($this->input->post('logistics')),
            'paymentWay' => trim($this->input->post('paymentWay')),
            'invoice' => trim($this->input->post('invoice')),
        ];

        //基本信息
        $data = [
            'name' => trim($this->input->post('name')),
            'source' => trim($this->input->post('source')),
            'type' => trim($this->input->post('type')),
            'purchaseCode' => trim($this->input->post('purchaseCode')),
            'createTime' => trim(strtotime($this->input->post('createTime'))),
            'content' => trim($this->input->post('content')),
            'needCode' => trim($this->input->post('needCode')),
            'supplier' => trim($this->input->post('supplier')),
            'endTime' => trim(strtotime($this->input->post('endTime'))),
            'createMan' => trim($this->input->post('createMan')),//基本信息

            'gid' => json_encode($this->input->post('goods')),  //商品信息

            'contractInfo' => json_encode($contracInfo,JSON_UNESCAPED_UNICODE), //合同信息
            'offerInfo'=> '文件附件',
            'prepay' => trim($this->input->post('prepay')),
            'payMan' => trim($this->input->post('payMan')),
        ];

        if($this->input->post('action') == '暂存'){
            //状态为0 待提交
            $data['status'] = 0;
            $data['onStatus'] = 0;
        }else{
            //状态为1 审核中
            $data['status'] = 1;
            $data['onStatus'] = 1;
        }

        $resultData =  $this->purchaseOrder_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/procurement/purchaseOrder');
        }
    }

    //确认支付功能
    public function purchaseEditStatus(){

        //确认付款之后获取到onStatus为1

        //然后更新status为2   onStatus为2

        //然后采购单那边在提交,status为1,  在确认付款的时候为onstatus+1

//        这边更新status和onstatus两个字段

//        print_r($_GET);
//        die();
        $onStatus = $this->input->get('onStatus');
        $status = $this->input->get('status');


        $data = [
            'status' => $onStatus + 1, //2
            'onStatus' => $status,
        ];

        $where = [
            'id' => $this->input->get('id')
        ];

//        print_r($data);
//        die();

        //是否会有添加失败的情况?
        $resultData =  $this->purchaseOrder_model->edit($data,$where);

//        print_r($resultData);
//        die();

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/check/index');
        }




    }

    //进货单编辑功能
    public function purchaseEdit(){

        print_r($_POST);


    }

    //进货单编辑页面
    public function purchaseEditView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $result = $this->purchaseOrder_model->getByFieldQueryData('*',$arg,0);
        $data['contractInfo'] = json_decode($result['contractInfo'],true);
        $data['list'] = $result;


//        print_r($data);
//        die();

        $this->load->view('/admin/procurement/purchaseOrder/edit',$data);
    }



}